package com.itcast.service.product.ipml;


import javax.persistence.Query;

import org.junit.Test;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itcast.service.base.DaoSupport;
import com.itcast.service.product.ProductTypeService;

@Service
@Transactional
public class ProductTypeServiceBean extends DaoSupport implements ProductTypeService {
	@Override
	public <T> void delete(Class<T> entityClass, Object[] entityids) {
		StringBuffer jpql=new StringBuffer();
		if(entityids !=null && entityids.length>0){
			for(int i=0;i<entityids.length;i++){
				jpql.append("?").append(i+2).append(",");
			}
		}
		jpql.deleteCharAt(jpql.length()-1);
		
		Query query=em.createQuery("update ProductType o set o.visible=?1 where o.typeid in ("+jpql.toString()+")")
				.setParameter(1, false);
		for(int i=0;i<entityids.length;i++){
			query.setParameter(i+2, entityids[i]);
		}
		query.executeUpdate();
	}
	
	@Override
	public <T> void delete(Class<T> entityClass, Object entityid) {

		em.createQuery("update ProductType o set o.visible=?1 where o.typeid="+(Integer)entityid).setParameter(1, false);
	}
	
	@Test 
	public void  testCodeLink(){
		StringBuffer jpql = new StringBuffer();

		for (int i = 0; i < 3; i++) {
			jpql.append("?").append(i + 2).append(",");
		}

		jpql.deleteCharAt(jpql.length()-1);
		System.out.println(jpql.toString());
	}

}
